import { get } from '@/http/axios'
import { Store } from 'vuex'

export type httpUrl = {
    url:string
    params:string
    base:string | undefined
}
type fjGc = {
    latest:Array<any>
    timeline:Array<any>
}

export function getMap () {
    const map = new Map()
        map.set('dynamicTv',{ 'url': '/x/web-interface/dynamic/region?ps=12&pn=1&rid=11','params':'archives' })
        map.set('film',{ 'url': '/x/web-interface/dynamic/region?ps=12&pn=1&rid=23','params':'archives' })
        map.set('JL',{ 'url': '/x/web-interface/dynamic/region?ps=12&pn=1&rid=177','params':'archives' })
        map.set('GC',{ 'url': '/pgc/web/timeline/v2?day_before=6&day_after=0&season_type=4','params':'result' }) //
        map.set('FJ',{ 'url': '/pgc/web/timeline/v2?day_before=6&day_after=0&season_type=1','params':'result' }) //
        map.set('ZY',{ 'url': '/pgc/web/variety/feed?cursor=0&page_size=14','params':'list' })
        map.set('KT',{ 'url': '/pugv/app/web/floor/switch?load_type=1','params':'season' })
        map.set('live',{ 'url': '/xlive/web-interface/v1/webMain/getMoreRecList?platform=web','params':'recommend_room_list','base':'/liveCard' })
    return map
} 


/**
 * 
 * @param urlObj 上面的map中的value
 * @param type map中的key
 * @param store
 */
export function getData(urlObj:httpUrl,type:string,store:Store<any>){
    const { url = 'no',params,base } : { url:string, params:string, base:string | undefined } = { ...urlObj }
    
    return new Promise((reslove,rej) => {
        get(url,'',base).then(res => {
            if (params !== 'result') {
                store.commit('setVideoAfterCardList',{ 'type':type,'arr':res.data[params] })
                reslove(atherArr(type,store))
            } else {
                store.commit('setVideoAfterCardList',{ 'type':type,'arr':res[params] })
                reslove(handleFjAndGC(type,store))
            }
        }).catch(err => {
            rej('错啦')
        })
    })
}
/**
 * 
 * @param type map的key 类型
 * @param store 
 * @returns 国创或番剧数据中的某条数据
 * 逻辑：
 *      首先 通过接口获取到数据后先会存入state的videoAfterCardList字段 类型为map key：类型 value：list值
 *      通过类型获取到value值
 *      先拿titmeline中的episodes -> list 字段
 *      titmeline 取完了再取latest
 *      最后全部没有了在重新加载数据
 */
export function handleFjAndGC(type:string,store:Store<any>){
    let maplist = store.state.videoAfterCardList
    let arrlist = maplist.get(type) as fjGc
    let arr
    let flag = false
    if (arrlist['timeline']) {
        arrlist['timeline'].forEach(el => {
            if (el.episodes.length && !flag) {
                arr = el.episodes.pop()
                flag = true
            }
        })
    }
    if (!flag) {
        arr = arrlist['latest'].pop()
    }
    if (!arrlist['latest'].length) {
        maplist.delete(type)
    }
    return arr
}
export function atherArr(type:string,store:Store<any>){
    let maplist = store.state.videoAfterCardList
    let arrlist = maplist.get(type) as Array<any>
    let arr = arrlist.pop()
    if (!arrlist.length) {       //取出一个以后 还有数据 就继续更新  没有了则删除元素 继续通过http获取新数据
        maplist.delete(type)
    }
    //console.log('最开始的时候',arr,arrlist,store.state.videoAfterCardList)
    return arr
}
